let url='http://localhost:8080/UserServlet'

//注册
function sign(userid,nc,passwd) {
    $.post(url,{
        type:"addZhuce",
        zh:userid,
        nc:nc,
        pwd:passwd
    },function (data) {
    })
}

//登录的所有用户信息
async function login() {
    let msg=null
    await $.post(url,function (data){
        msg=data.data
    },"JSON")
    return msg
}

//微博登录
async function weiLogin(){
    let msg
    await $.ajax({
        url:"http://localhost:8080/UserServlet",
        type:"get",
        data:{
                type:"weibologin",
             },
        xhrFields:{withCredentials:true},
        crossDomain:true,
        success:function (data) {
            msg=JSON.parse(data)
        }
    })
    return msg
}

//获取当前用户信息
async function userinfo(id) {
    alert(id)
    let msg=null
    await $.post(url, {
        type:"selectInfoById",
        id:id
    },function (data){
        msg=data.data
    },"JSON")
    return msg
}

//更改个人信息
function save(id,nc,tx,sex,zy,city,info) {
    $.post(url,{
        type:'updateZhuce',
        id:id,
        nc:nc,
        tx:tx,
        sex:sex,
        zy:zy,
        city:city,
        info:info
    },function (data) {
    })
}

//邮箱发送验证
async function email(zh,email) {
    let msg
    await $.post(
        "http://localhost:8080/smailcode",
        {
            username:zh,
            email:email
        },function (data) {
            msg=data
        }
    )
    return msg
}
//绑定邮箱
async function updateEmail(zh,email) {
    let msg
    await $.post(
        url,
        {
            type:'updateEmail',
            zh:zh,
            email:email
        },function (data) {
            msg = data.code
        },
        "JSON"
    )
    return msg
}
//修改密码
function upPwd(zh,pwd) {

    $.post(url,{
        type:'updatePwdById',
        zh:zh,
        pwd:pwd
    },function (data) {
    },"JSON")
}

//修改密码
async function updatePwd(zh,pwd) {
    let msg
    await $.post(url,{
        type:'updatepwd',
        zh:zh,
        pwd:pwd
    },function (data) {
        msg=data.code
    },"JSON")
    return msg
}

//上传头像地址
function upTx(imgUrl,zh) {
    $.post(url,{
        type:'updateTx',
        zh:zh,
        tx:imgUrl
    },function (data) {
    },"JSON")
}

//手机发送验证码
async function sendPhone(phone) {
    // let msg
    // await $.get("http://localhost:8080/sphonecode",{
    //     number:phone
    // },function (data) {
    //     msg=JSON.parse(data)
    // })
    // alert(msg)
    // return msg
    let msg
    await $.ajax({
        url:"http://localhost:8080/sphonecode",
        type:"get",
        data:{ number:phone},
        xhrFields:{withCredentials:true},
        crossDomain:true,
        success:function (data) {
            msg=JSON.parse(data)
        }
    })
    alert(msg)
    return msg
}
//手机发送验证码
async function verifyPhone(phone,num) {
    // let msg
    // await $.get("http://localhost:8080/cphonecode",{
    //     number:phone,
    //     verifycode:num
    // },function (data) {
    //     alert(data)
    //     msg=JSON.parse(data)
    // })
    // return msg
    let msg
    await $.ajax({
        url:"http://localhost:8080/cphonecode",
        type:"get",
        data:{ number:phone,verifycode:num},
        xhrFields:{withCredentials:true},
        crossDomain:true,
        success:function (data) {
            msg=JSON.parse(data)
        }

    })
    return msg
}
//绑定手机号
async function upPhone(zh,phone){
    let msg
    await $.post(
        url,
        {
            type:'updatePhone',
            zh:zh,
            phone:phone
        },function (data) {
            msg = data.code
        },
        "JSON"
    )
    return msg
}


//关注表的信息
async function careInfo() {
    let msg
    await $.post('http://localhost:8080/CareServlet',{ },function (data) {
        msg=data
    },'JSON')
    return msg
}

//身份验证
async function verIdandName(id,name) {
    let msg
    await $.post('http://localhost:8080/identity',{
        carid:id,
        name:name
    },function (data) {
        msg=JSON.parse(data)
    })
    return msg
}

//存入身份信息
async function saveIdandName(zh,id,name) {
    let msg
    await $.post(url,{
        type:'updateNameAndId',
        zh:zh,
        name:name,
        id_card:id
    },function (data) {
        msg = data
    },"JSON")
    return msg
}

//上传用户信息
// async function alluserinfo(userinfo) {
//     let msg
//     await $.post(url,{
//         type:'',
//         userinfo:userinfo,
//     },function (data) {
//         msg = data
//     },"JSON")
//     return msg
// }




//-----------------------------------------------------------------------------------------------------------

//初始化
function initData() {
    setCookie('name','Release发来的','s20')
}
function getUser() {
    alert(this.name)
    this.name=getCookie('name')
    alert(this.name)
}
//读取cookies
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if(arr = document.cookie.match(reg)) return unescape(arr[2]);
    else return null;
}
//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if(cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
//设置cookie和过期时间
function setCookie(name, value, time) {
    var strsec = getsec(time);
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec * 1);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//str:时间单位 s秒、h时、d天、m分
function getsec(str) {
    var str1 = str.substring(1, str.length) * 1;
    var str2 = str.substring(0, 1);
    if(str2 == "s") {
        return str1 * 1000;
    } else if(str2 == "h") {
        return str1 * 60 * 60 * 1000;
    } else if(str2 == "d") {
        return str1 * 24 * 60 * 60 * 1000;
    }else if(str2 == "m") {
        return str1 *  60 * 1000;
    }
}

export default {
    sign,
    login,
    save,
    userinfo,
    email,
    updateEmail,
    upPwd,
    upTx,
    sendPhone,
    verifyPhone,
    careInfo,
    verIdandName,
    saveIdandName,
    upPhone,
    updatePwd,
    weiLogin
}